package com.ujiuye.dao;

import com.ujiuye.pojo.Student;
import com.ujiuye.utils.MyUtils;
import com.ujiuye.utils.PageTools;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class StudentDao {

    /**
     * 查询条数
     * @param search
     * @return
     */
    public int queryAllSizeBySearch(String search) {
        String sql = "select count(*) from student where sname like '%" + search + "%'";
        try {
            long l = (long)MyUtils.qr.query(sql,new ScalarHandler());
            return (int)l;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 分页查询
     * @param pt
     * @param search
     * @return
     */
    public List<Student> dimQuery(PageTools pt, String search) {
        String sql = "select * from student ";
        //判断是否传递了条件
        if(search != null && !search.equals("")) {
            sql += " where sname like '%" + search + "%' ";
        }

        sql += "limit ?,?";
        try {
            return MyUtils.qr.query(sql,new BeanListHandler<>(Student.class),pt.getIndex(),pt.getPageSize());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;

    }
}
